package cn.infinite.security.handler;

import cn.easier.club.base.dto.ResultInfo;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class DefaultSmsAuthenticationFailureHandler implements SmsAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        log.error("短信认证失败=>{}", exception.getMessage());
        response.setStatus(HttpStatus.SC_OK);
        ResultInfo<String> result = new ResultInfo<>("error", exception.getMessage(), null);
        response.getWriter().write(JSON.toJSONString(result));
        response.getWriter().flush();
        response.getWriter().close();
    }
}
